import table from "./table";
import useLoad from './load';
import {WebStorage} from 'jboot-env';

const whiteList = ['/login', '/permission/fail', '/collection/qrCode'];

export default function (config, options) {
    const { http, store } = options.getInstance();

    const {loadUser, loadButtons} = useLoad(http, store);

    config.table(table).check(function (permissionCode, to, callback) {
        if(whiteList.filter(item => to.path.startsWith(item)).length > 0){
            callback();
        }else{
            WebStorage.localMode().get('{PROJECT_NAME}-token')
                .then(loadUser)
                .then(loadButtons) //加载按钮权限，不需要可删除。
                .then(() => callback())
                .catch(() => callback('login'));
        }
    }).after(function (route) {
        document.title = [route.meta?.title, process.env.VUE_APP_PROJECT_NAME].filter(Boolean).join(' | ');
    });
}
